/*
 * @lc app=leetcode.cn id=1475 lang=javascript
 *
 * [1475] 商品折扣后的最终价格
 */

// @lc code=start
/**
 * @param {number[]} prices
 * @return {number[]}
 */
var finalPrices = function (prices) {
  let res = new Array(prices.length).fill(0);
  let stack = [];

  for(let i = prices.length - 1; i >=0 ; i--) {
    const price = prices[i];
    while(stack.length && stack[stack.length - 1] > price) {
      stack.pop();
    }

    res[i] = price - (stack[stack.length - 1] || 0);

    stack.push(price);
  }
  return res;
};
// @lc code=end
